package hdelros

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-flow/cache"
	"logi-flow/models"
	"logi-flow/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	name := c.PostForm("name")
	passes := c.PostForm("passes")

	if name == "" || passes == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"smg":  "字段不能为空",
		})
		return
	}
	redis_key := "redis_key" + name
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if po >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次,禁止登录",
		})
		return
	}
	var user models.User
	models.DB.Where("name=?", name).First(&user)
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户密码错误",
		})
		return
	}
	if user.Passes != pkg.Md5(passes) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if po == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次,禁止2个小时登录",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func UserRole(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))

	data := models.UserRole{
		Uid: uid,
		Rid: rid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func RoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))

	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func RoleList(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.rid = roles.id
		  WHERE
		  user_roles.uid=1
	*/
	var roles []models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid=?", uid).Find(&roles)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": roles,
	})
	return
}
func NodeList(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.nid = nodes.id
		  WHERE
		  role_nodes.rid=1
	*/
	var node []models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").
		Where("role_nodes.rid=?", rid).Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return
}

func Courier(c *gin.Context) {
	CourierCode := c.PostForm("courier_code")
	CourierName := c.PostForm("courier_name")

	if CourierCode == "" || CourierName == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Courier{
		CourierCode: CourierCode,
		CourierName: CourierName,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func Order(c *gin.Context) {
	OrderCode := c.PostForm("order_code")
	OrderName := c.PostForm("order_name")
	OrderTo := c.PostForm("order_to")
	Cid, _ := strconv.Atoi(c.PostForm("cid"))

	if OrderCode == "" || OrderName == "" || OrderTo == "" || Cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		OrderCode: OrderCode,
		OrderName: OrderName,
		OrderTo:   OrderTo,
		Cid:       Cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	/*
		SELECT
		  orders.id as OrderId,
		  couriers.courier_code as CourierCode,
		  couriers.courier_name as CourierName,
		  orders.order_code as OrderCode,
		  orders.order_name as OrderName,
		  orders.order_to as OrderTo
		FROM
		  orders
		  LEFT JOIN couriers ON couriers.id = orders.cid
	*/
	var list []models.OrderList

	redis_key := "redis_list" + strconv.Itoa(page)
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if po == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"couriers.courier_code as CourierCode",
				"couriers.courier_name as CourierName",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"orders.order_to as OrderTo",
			).
			Joins("LEFT JOIN couriers ON couriers.id = orders.cid").
			Offset(offset).Limit(size).Find(&list)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}

	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"list":  list,
		},
	})
	return

}
func OrderXq(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  orders.id as OrderId,
		  couriers.courier_code as CourierCode,
		  couriers.courier_name as CourierName,
		  orders.order_code as OrderCode,
		  orders.order_name as OrderName,
		  orders.order_to as OrderTo
		FROM
		  orders
		  LEFT JOIN couriers ON couriers.id = orders.cid
		  WHERE
		  orders.id=1
	*/
	var list models.OrderList

	redis_key := "redis_xq" + id
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if po == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"couriers.courier_code as CourierCode",
				"couriers.courier_name as CourierName",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"orders.order_to as OrderTo",
			).
			Joins("LEFT JOIN couriers ON couriers.id = orders.cid").
			Where("orders.id=?", id).Find(&list)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}

func UserRoles(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))

	data := models.UserRole{
		Uid: uid,
		Rid: rid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func RoleNodes(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))

	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func RoleLists(c *gin.Context) {
	uid := c.Query("uid")
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON user_roles.rid = roles.id
		  WHERE
		  user_roles.uid=1
	*/
	var roles []models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON user_roles.rid = roles.id").
		Where("user_roles.uid=?", uid).Find(&roles)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": roles,
	})
	return
}
func NodeLists(c *gin.Context) {
	rid := c.Query("rid")
	/*
		SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON role_nodes.nid = nodes.id
		  WHERE
		  role_nodes.rid=1
	*/
	var node []models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON role_nodes.nid = nodes.id").
		Where("role_nodes.rid=?", rid).Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": node,
	})
	return
}

func Couriers(c *gin.Context) {
	CourierCode := c.PostForm("courier_code")
	CourierName := c.PostForm("courier_name")

	if CourierCode == "" || CourierName == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Courier{
		CourierCode: CourierCode,
		CourierName: CourierName,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func Orders(c *gin.Context) {
	OrderCode := c.PostForm("order_code")
	OrderName := c.PostForm("order_name")
	OrderTo := c.PostForm("order_to")
	Cid, _ := strconv.Atoi(c.PostForm("cid"))

	if OrderCode == "" || OrderName == "" || OrderTo == "" || Cid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		OrderCode: OrderCode,
		OrderName: OrderName,
		OrderTo:   OrderTo,
		Cid:       Cid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderLists(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	/*
		SELECT
		  orders.id as OrderId,
		  couriers.courier_code as CourierCode,
		  couriers.courier_name as CourierName,
		  orders.order_code as OrderCode,
		  orders.order_name as OrderName,
		  orders.order_to as OrderTo
		FROM
		  orders
		  LEFT JOIN couriers ON couriers.id = orders.cid
	*/
	var list []models.OrderList

	redis_key := "redis_list" + strconv.Itoa(page)
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if po == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"couriers.courier_code as CourierCode",
				"couriers.courier_name as CourierName",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"orders.order_to as OrderTo",
			).
			Joins("LEFT JOIN couriers ON couriers.id = orders.cid").
			Offset(offset).Limit(size).Find(&list)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}

	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"list":  list,
		},
	})
	return

}
func OrderXqs(c *gin.Context) {
	id := c.Query("id")
	/*
		SELECT
		  orders.id as OrderId,
		  couriers.courier_code as CourierCode,
		  couriers.courier_name as CourierName,
		  orders.order_code as OrderCode,
		  orders.order_name as OrderName,
		  orders.order_to as OrderTo
		FROM
		  orders
		  LEFT JOIN couriers ON couriers.id = orders.cid
		  WHERE
		  orders.id=1
	*/
	var list models.OrderList

	redis_key := "redis_xq" + id
	po, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if po == "" {
		models.DB.Table("orders").
			Select(
				"orders.id as OrderId",
				"couriers.courier_code as CourierCode",
				"couriers.courier_name as CourierName",
				"orders.order_code as OrderCode",
				"orders.order_name as OrderName",
				"orders.order_to as OrderTo",
			).
			Joins("LEFT JOIN couriers ON couriers.id = orders.cid").
			Where("orders.id=?", id).Find(&list)
		marshal, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(po), &list)
	}

	var count int64
	models.DB.Model(&models.Order{}).Count(&count)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
